<?php
App::uses('AppModel', 'Model');
App::uses('SimplePasswordHasher', 'Controller/Component/Auth');
/**
 * Utilisateur Model
 *
 * @property Role $Role
 * @property Country $Country
 * @property Cercle $Cercle
 * @property Exemplaire $Exemplaire
 * @property MembreCercle $MembreCercle
 */
class Utilisateur extends AppModel {


	//The Associations below have been created with all possible keys, those that are not needed can be removed

/**
 * belongsTo associations
 *
 * @var array
 */
	public $belongsTo = array(
		'Role' => array(
			'className' => 'Role',
			'foreignKey' => 'role_id',
			'conditions' => '',
			'fields' => '',
			'order' => ''
		),
		'Country' => array(
			'className' => 'Country',
			'foreignKey' => 'country_id',
			'conditions' => '',
			'fields' => '',
			'order' => ''
		)
	);

/**
 * hasMany associations
 *
 * @var array
 */
	public $hasMany = array(
		'Cercle' => array(
			'className' => 'Cercle',
			'foreignKey' => 'utilisateur_id',
			'dependent' => false,
			'conditions' => '',
			'fields' => '',
			'order' => '',
			'limit' => '',
			'offset' => '',
			'exclusive' => '',
			'finderQuery' => '',
			'counterQuery' => ''
		),
		'Exemplaire' => array(
			'className' => 'Exemplaire',
			'foreignKey' => 'utilisateur_id',
			'dependent' => false,
			'conditions' => '',
			'fields' => '',
			'order' => '',
			'limit' => '',
			'offset' => '',
			'exclusive' => '',
			'finderQuery' => '',
			'counterQuery' => ''
		)
	);
	
	/**
     * Before Save
     * @param array $options
     * @return boolean
     */
     public function beforeSave($options = array()) {
        if (!empty($this->data[$this->alias]['password'])) {
            $passwordHasher = new SimplePasswordHasher(array('hashType' => 'sha256'));
            $this->data[$this->alias]['password'] = $passwordHasher->hash(
                $this->data[$this->alias]['password']
            );
        }
        return true;
    }
	
	public $validate = array(
        'nom_utilisateur' => array( 
			'required' => array(
				'rule' => 'notEmpty',
				'required' => 'create'
			),
            'alphaNumeric' => array(
                'rule'     => 'alphaNumeric',
                'message'  => 'Chiffres et lettres uniquement !'
            ),
            'between' => array(
                'rule'    => array('between', 5, 15),
                'message' => 'Entre 5 et 15 caractères'
            ),
			'uniqueUsername' => array(
				'rule'    => array('uniqueUsername'),
				'message' => 'Ce pseudonyme existe déjà.'
			)
        ),
        'password' => array(
            'rule'    	=> array('minLength', '8'),
			'required' 	=> 'create',
            'message'	=> '8 caractères minimum'
        ),
        'password_verif' => array(
			'required' => array(
				'rule' => 'notEmpty',
				'required' => 'create'
			),
            'minLength' => array(
				'rule'    	=> array('minLength', '8'),
				'message'	=> '8 caractères minimum'
            ),
            'equalToPassword' => array(
                'rule'    => array('equalToPassword'),
                'message' => 'Les 2 mots de passe ne sont pas identiques'
            )
        ),
        'email' => 'email',
        'country_id' => array(
			'rule' => 'checkCountry',
			'message'    => 'Veuillez choisir un pays',
			'required' => true
		),
        'code_postal' => array(
            'rule'    	=> array('minLength', '4'),
			'required' 	=> true,
            'message'	=> 'Veuillez saisir un code postal'
        ),
        'ville' => array(
            'rule'    	=> array('minLength', '1'),
			'required' 	=> true,
            'message'	=> 'Veuillez saisir une ville'
        ),
        'adresse_1' => array(
            'rule'    	=> array('minLength', '1'),
			'required' 	=> true,
            'message'	=> 'Veuillez saisir une adresse'
        ),
        'nom' => array(
            'rule'    	=> array('minLength', '1'),
			'required' 	=> true,
            'message'	=> 'Veuillez saisir votre nom'
        ),
        'prenom'=> array(
            'rule'    	=> array('minLength', '1'),
			'required' 	=> true,
            'message'	=> 'Veuillez saisir votre prénom'
        ),
        'date_de_naissance' => array(
            'rule'       => 'date',
            'message'    => 'Entrez une date valide',
            'required' => true
        )
    );

    public function uniqueUsername($check) {
         $nombre = $this->find('count', array(
            'conditions' => $check,
            'recursive' => -1
        ));
        return $nombre < 1;
    }
	
    public function equalToPassword($check) {
        return $this->data['Utilisateur']['password']==$this->data['Utilisateur']['password_verif'];
    }
	
    public function checkCountry($check) {
	/** TODO **/
         // $nombre = $this->find('count', array(
            // 'conditions' => $check,
            // 'recursive' => -1
        // ));
        return true;
    }
	
}
